package com.zjitc.book.controller;

import com.zjitc.book.entity.Book;
import com.zjitc.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
//@CrossOrigin
public class BookController {
    @Autowired
    private BookService bookService;


    @GetMapping("/books")
    @PreAuthorize("hasAuthority('book:list')")
    public List<Book> getBooks(){
        return bookService.getBooks();
    }

    @GetMapping("/books/{id}")
    @PreAuthorize("hasAuthority('book:list')")
    public Book getBookById(@PathVariable Integer id){
        return bookService.getBooksById(id);
    }

    @GetMapping("/books/search")
    @PreAuthorize("hasAuthority('book:list')")
    public List<Book> searchBooksByCondition(Book book){
        return bookService.searchBooksByCondition(book);
    }

    @DeleteMapping("/books/{id}")
    @PreAuthorize("hasAuthority('book')")
    public int deleteBookById(@PathVariable Integer id){
        return bookService.deleteBookById(id);
    }

    @PutMapping("/books/{id}")
    @PreAuthorize("hasAuthority('book')")
    public int updateBookById(@PathVariable Integer id,@RequestBody Book book){
        return bookService.updateBookById(id,book);
    }

    @PostMapping("/books")
    @PreAuthorize("hasAuthority('book:add')")
    public int insertBook(@RequestBody Book book){
        return bookService.insertBook(book);
    }
}
